<script setup lang="ts">
import type { StudentWordItem } from '@/api/types/student'
import StudyWordCard from '@/components/WordCard/StudyWordCard.vue'
import { ref } from 'vue'

interface WordWithStatus extends StudentWordItem {
  isKnown: boolean
}

const props = defineProps<{
  words: StudentWordItem[]
}>()

const emit = defineEmits<{
  (e: 'update:words', words: WordWithStatus[]): void
  (e: 'onStart', words: WordWithStatus[]): void
}>()

const wordList = ref<WordWithStatus[]>(props.words.map(word => ({
  ...word,
  isKnown: false,
})))

const selectedCount = ref(0)
const activeWordId = ref<string | number | null>(null)

function handleKnownToggle(word: WordWithStatus) {
  word.isKnown = !word.isKnown
  selectedCount.value = word.isKnown ? selectedCount.value + 1 : selectedCount.value - 1
  emit('update:words', wordList.value)
}

function handleWordActivate(wordId: string | number) {
  activeWordId.value = wordId
}

function handleStartLearning() {
  emit('onStart', wordList.value)
}

function showKnownIcon(word: WordWithStatus) {
  return word.isKnown || isActive(word)
}

function isActive(word: WordWithStatus) {
  return activeWordId.value === word.wordId
}
</script>

<template>
  <view class="select-known-words">
    <view class="word-list">
      <view v-for="word in wordList" :key="word.wordId" class="word-item">
        <StudyWordCard
          type="row" :word="word" :is-active="isActive(word)"
          @activate="handleWordActivate(word.wordId)"
        />
        <view v-if="showKnownIcon(word)" class="known-icon" :class="{ active: word.isKnown }" @tap.stop="handleKnownToggle(word)">
          <image v-if="!word.isKnown" src="@/static/images/wordStudy/sc-icon.png" mode="widthFix" />
          <image v-else src="@/static/images/wordStudy/sc-act-icon.png" mode="widthFix" />

          <text v-if="!word.isKnown">
            标为熟词
          </text>
          <text v-else>
            熟词
          </text>
        </view>
      </view>
    </view>

    <view class="footer" @tap="handleStartLearning">
      开始背单词
    </view>
  </view>
</template>

<style lang="scss" scoped>
.select-known-words {
    padding: 0 30rpx;
    .selected-count {
        font-size: 30rpx;
        color: #FF8E00;
        font-weight: bold;
    }

    .word-list {
        padding-bottom: 180rpx;

        .word-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 2rpx solid #f5f5f5;
            position: relative;
            margin-bottom: 20rpx;

            .word-card{
                padding-right: 100rpx;
                min-height: 80rpx;
            }

            .known-icon {
                position: absolute;
                right: 20rpx;
                color: #999;
                display: flex;
                align-items: center;
                justify-content: center;
                flex-direction: column;
                cursor: pointer;
                border-radius: 50%;
                transition: all 0.3s ease;
                width: 80rpx;

                image{
                    width: 40rpx;
                    height: 40rpx;
                }

                text{
                    font-size: 20rpx;
                }

            }
        }
    }

    .footer {
        position: fixed;
        left: 0;
        cursor: pointer;
        bottom: 0;
        width: 100%;
        background-color: $primary-highlight-color;
        padding: 30rpx 0;
        text-align: center;
        color: #fff;
        font-size: 30rpx;
        font-weight: bold;

        .selected-info {
            justify-content: space-between;
            font-size: 28rpx;
            flex: 1;
            color: #666;
        }

        .start-btn {
            width: 200rpx;
            height: 80rpx;
            line-height: 80rpx;
            text-align: center;
            background-color: #EEAC3A;
            color: #fff;
            border-radius: 10rpx;
            font-size: 32rpx;
        }
    }
}
</style>
